/*
 * Simple test file for cxxomfort's 
 * system_error  feature backports.
 * 
 * This example draws from the example posted in 
 * https://en.cppreference.com/w/cpp/error/generic_category
 *
 */

#include <cxxomfort/base.hpp>
#include <cxxomfort/system_error.hpp>
#include <cxxomfort/string_view.hpp>
#include <cxxomfort/ostream.hpp>
#include <iostream>
#include <cerrno>
#include <string>
#include <fstream>
#include <cxxomfort/library/type_name.hpp>

int main()
{
    using namespace std;
    using cxxomfort::library::type_name;
    
    cxxomfort::output_info();
    cout<< endl;
    cout<< "error_category is: "<< type_name<error_category>()<< endl;
    error_category const& ecat1 = system_category();
    error_category const& ecat2 = generic_category();
    cout<< "system == generic? "<< (ecat1==ecat2)<< endl;

    error_condition gcond = generic_category().default_error_condition(ENOENT);
    cout<< "Generic  :"<< endl;
    cout<< "Category : " << gcond.category().name() << '\n'
        << "Value    : " << gcond.value() << '\n'
        << "Message  : " << gcond.message() << '\n';

    error_condition scond = system_category().default_error_condition(EDOM);
    cout<< "System   :"<< endl;
    cout<< "Category : " << scond.category().name() << '\n'
        << "Value    : " << scond.value() << '\n'
        << "Message  : " << scond.message() << '\n';


    cout<< "Malformed error code:"<< endl;
    error_condition wrongcond = system_category().default_error_condition(10001);
    cout<< "Category: " << wrongcond.category().name() << '\n'
        << "Value: " << wrongcond.value() << '\n'
        << "Message: " << wrongcond.message() << '\n';
       
    cout<< "\n"
		<< "Making an error_code..."<< flush;
	error_code ec = make_error_code(errc::identifier_removed);
	cout<< "Done"<< endl;
    cout<< ec<< " , "<< ec.message()<< endl;
	cout<< make_error_code(cxxomfort::fix::to_errc(14))<< endl;
    
    cout<< endl;
    cout<< "Type of std::errc       : "<< 
#if (defined(CXXO_FIX_broken_msvc_errc))
	"bad (declares a namespace)"
#else
	type_name<std::errc>()
#endif
	<< endl;
    cout<< "Type of cxxomfort errc  : "<< type_name<cxxomfort::fix::errc>()<< endl;
    cxxomfort::fix::errc ex;
    (void)ex;


try {

    cout<< "Trying to trigger an error."<< endl;

    throw system_error(EBUSY, system_category());

}

catch (std::system_error const& E) {
        cout << "Caught a system_error\n";
        cout << "Code "<< E.code()<< " , dynamic type "<< cxxomfort::library::typeid_demangle(typeid(E))<< endl;
        cout<< " Error description " << E.what() << endl;
}
catch (std::exception const& E) {
        cout<< "Caught a generic exception "<< E.what()<< endl;
        cout<< "Dynamic exception type: "<< cxxomfort::library::typeid_demangle(typeid(E))<< endl;
} catch (...) {
        cout<< "Unknown exception"<< endl;
}
	

}

